package com.example.administrator.printoutindemo.mvp.ui.view;

import android.app.DatePickerDialog;
import android.content.Context;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.example.administrator.printoutindemo.R;
import com.example.administrator.printoutindemo.mvp.ui.entity.GetStoreForLeaderResult;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.utils.SharedPreferencesHelper;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import me.jessyan.armscomponent.commonres.view.NiceSpinner;

/**
 * 属性选择的布局及逻辑
 */
public class ChuKuQingKuangRightSideslipLay extends RelativeLayout implements View.OnClickListener {
    protected SharedPreferencesHelper mSharedPreferencesHelper;
    private Context mCtx;
    private EditText et_ding_dan_hao;
    private NiceSpinner ns_cang_ku;
    private EditText et_xiang_mu_ming_cheng;
    private EditText et_xiang_mu_bian_hao;
    private EditText et_wu_liao_ming_cheng;
    private TextView et_kai_shi_shi_jian;
    private TextView et_jie_shu_shi_jian;
    private EditText et_gong_ying_shang;
    private EditText et_mu_di_xiang_mu;
    private FlowingDrawerInterface mFlowingDrawerInterface;
    /**
     * 创建PopupWindow
     */
    private PopupWindow mMenuPop;
    private CloseMenuCallBack menuCallBack;
    private String mUserId;
    private String mSname;
    private String mSid;
    private String methodName = "GetStoreForLeader";

    public ChuKuQingKuangRightSideslipLay(Context context, FlowingDrawerInterface flowingDrawerInterface, String user_id) {
        super(context);
        mCtx = context;
        this.mFlowingDrawerInterface = flowingDrawerInterface;
        this.mUserId = user_id;
        inflateView();

    }

    private void inflateView() {
        View view = View.inflate(getContext(), R.layout.fragment_menu_chu_ku_qing_kuang, this);
        Toolbar toolbar = view.findViewById(R.id.toolbar);
        TextView tvTitle = toolbar.findViewById(R.id.title);

        Button btn_qu_xiao = view.findViewById(R.id.btn_qu_xiao);
        Button btn_que_ding = view.findViewById(R.id.btn_que_ding);
        ns_cang_ku = view.findViewById(R.id.ns_cang_ku);
        et_ding_dan_hao = view.findViewById(R.id.et_ding_dan_hao);
        et_xiang_mu_ming_cheng = view.findViewById(R.id.et_xiang_mu_ming_cheng);
        et_xiang_mu_bian_hao = view.findViewById(R.id.et_xiang_mu_bian_hao);
        et_wu_liao_ming_cheng = view.findViewById(R.id.et_wu_liao_ming_cheng);
        et_kai_shi_shi_jian = view.findViewById(R.id.et_kai_shi_shi_jian);
        et_jie_shu_shi_jian = view.findViewById(R.id.et_jie_shu_shi_jian);
        et_gong_ying_shang = view.findViewById(R.id.et_gong_ying_shang);
        et_mu_di_xiang_mu = view.findViewById(R.id.et_mu_di_xiang_mu);
        et_kai_shi_shi_jian.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showDateDialog(et_kai_shi_shi_jian, 0);
            }
        });
        et_jie_shu_shi_jian.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showDateDialog(et_jie_shu_shi_jian, 1);

            }
        });
        tvTitle.setText("筛选");
        toolbar.setNavigationOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mFlowingDrawerInterface.flowingDrawerClose();
            }
        });
        btn_qu_xiao.setOnClickListener(this);
        btn_que_ding.setOnClickListener(this);
    }

    private void showDateDialog(TextView textView, int type) {
        DatePickerDialog dialog = null;
        Calendar calendar = Calendar.getInstance();

        DatePickerDialog.OnDateSetListener dateListener1 = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(android.widget.DatePicker datePicker, int year,
                                  int month, int dayOfMonth) {
                // Calendar月份是从0开始,所以month要加1
                if (type == 0) {
                    textView.setText(new StringBuilder().append(year).append("-").append(month + 1).append("-").append(dayOfMonth).append(" 00:00:00").toString());
                } else {
                    textView.setText(new StringBuilder().append(year).append("-").append(month + 1).append("-").append(dayOfMonth).append(" 23:59:59").toString());
                }
            }
        };
        dialog = new DatePickerDialog(mCtx, dateListener1,
                calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }

    public ChuKuQingKuangRightSideslipLay(Context context, FlowingDrawerInterface flowingDrawerInterface, String user_id, SharedPreferencesHelper sharedPreferencesHelper, String chuKuQingKuangType) {
        super(context);
        mCtx = context;
        this.mFlowingDrawerInterface = flowingDrawerInterface;
        this.mSharedPreferencesHelper = sharedPreferencesHelper;
        this.mUserId = user_id;
        inflateView();
        getData(mUserId, chuKuQingKuangType);

    }

    private void getData(String user_id, String chuKuQingKuangType) {


        if (chuKuQingKuangType.equals("ld")) {
            methodName = "GetStoreForLeader";
        } else if (chuKuQingKuangType.equals("cgy")) {
            methodName = "GetStoreForCgy";
        }

        SoapUtil soapUtil = SoapUtil.getInstance(mCtx);
        soapUtil.setDotNet(true);
        SoapParams params = new SoapParams();
        params.put("user_id", user_id);

        String url = "http://" + mSharedPreferencesHelper.getString(BaseConstants.IP) + ":"
                + mSharedPreferencesHelper.getString(BaseConstants.PORT)
                + "/StoreService.asmx";
        /**
         * 请求
         */
        soapUtil.callEntry(url, BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}")) {


                    try {
                        List<GetStoreForLeaderResult> getStoreForLeaderResultList = new Gson().fromJson(result, new TypeToken<List<GetStoreForLeaderResult>>() {
                        }.getType());

                        bindCangKuData(getStoreForLeaderResultList);
                    } catch (JsonSyntaxException e) {
                        e.printStackTrace();
                    }
                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
            }
        }, getAapiUserName(), getApiPwd());


    }

    // 绑定仓库信息
    private void bindCangKuData(List<GetStoreForLeaderResult> getStoreForLeaderResultList) {
        ns_cang_ku.setTextColor(getResources().getColor(R.color.gray_text));

        ArrayList<String> dataNames = new ArrayList();
        ArrayList<String> dataSids = new ArrayList();
        for (int i = 0; i < getStoreForLeaderResultList.size(); i++) {
            dataNames.add(getStoreForLeaderResultList.get(i).getSname());
            dataSids.add(String.valueOf(getStoreForLeaderResultList.get(i).getSid()));
        }
        ns_cang_ku.attachDataSource(dataNames);
        mSname = dataNames.get(0);
        mSid = dataSids.get(0);

        ns_cang_ku.addOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mSname = dataNames.get(position);
                mSid = dataSids.get(position);
            }
        });

    }

    protected String getAapiUserName() {
        return mSharedPreferencesHelper.getString(BaseConstants.API_USER_NAME);
    }

    protected String getApiPwd() {
        return mSharedPreferencesHelper.getString(BaseConstants.API_PWD);
    }

    public ChuKuQingKuangRightSideslipLay(Context context, FlowingDrawerInterface flowingDrawerInterface) {
        super(context);
        mCtx = context;
        this.mFlowingDrawerInterface = flowingDrawerInterface;
        inflateView();

    }

    /***
     * 获取PopupWindow实例
     */
    private void getPopupWindow() {
        if (mMenuPop != null) {
            dismissMenuPop();
            return;
        } else {
        }
    }

    /**
     * 关闭窗口
     */
    private void dismissMenuPop() {
        if (mMenuPop != null) {
            mMenuPop.dismiss();
            mMenuPop = null;
        }

    }

    public void setCloseMenuCallBack(CloseMenuCallBack menuCallBack) {
        this.menuCallBack = menuCallBack;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_qu_xiao:
                mFlowingDrawerInterface.flowingDrawerClose();
                break;
            case R.id.btn_que_ding:
                String dingDanHao = et_ding_dan_hao.getText().toString().trim();
                String xiangMuMingCheng = et_xiang_mu_ming_cheng.getText().toString().trim();
                String xiangMuBianHao = et_xiang_mu_bian_hao.getText().toString().trim();
                String wuLiaoMingCheng = et_wu_liao_ming_cheng.getText().toString().trim();
                String gongYingShang = et_gong_ying_shang.getText().toString().trim();
                String muDiXiangMu = et_mu_di_xiang_mu.getText().toString().trim();
                String kaiShiShiJian = et_kai_shi_shi_jian.getText().toString().trim();
                String jieShuShiJian = et_jie_shu_shi_jian.getText().toString().trim();
                mFlowingDrawerInterface.search(mSid, xiangMuBianHao, xiangMuMingCheng, dingDanHao, wuLiaoMingCheng, gongYingShang, muDiXiangMu, kaiShiShiJian, jieShuShiJian);
                break;
        }
    }

    public interface FlowingDrawerInterface {

        void flowingDrawerClose();

        void search(String sid, String proid, String projectname, String purchaseid, String material, String suppliername, String targetproject, String outBeginTime, String outEndTime);

    }

    public interface CloseMenuCallBack {
        void setupCloseMean();
    }


}
